package com.gljx.web.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gljx.util.Axamine;
import com.gljx.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gljx.util.BigDecimalFormat;
import com.gljx.util.GetFid;
import com.gljx.web.Entity.OtherRefundBill;
import com.gljx.web.Entity.OtherRefundBillEntry;
import com.gljx.web.Entity.User;
import com.gljx.web.dao.CommonDao;
import com.gljx.web.dao.OtherRefundBillEntryMapper;
import com.gljx.web.dao.OtherRefundBillMapper;
import com.gljx.web.service.OtherRefundBillService;

@Service
@Transactional
public class OtherRefundBillServiceImpl extends CommonDao implements OtherRefundBillService{

	@Autowired
	private OtherRefundBillMapper otherRefundBillMapper;
	
	@Autowired
	private OtherRefundBillEntryMapper otherRefundBillEntryMapper;
	
	/**
	 *取分页数据    dmz 20160921
	 *@param pindex 页码
	 *@param psql SQL语句，需要带 rownum rn 例如：select rownum rn,t.* from zzda t
	 *@param psize 每页记录数
	 * */
	@Override
	public Map<String, Object> selectPage(int pindex, String psql, int psize) {
		  Map<String, Object> paramMap = new HashMap<String, Object>();
	       paramMap.put("Pindex", pindex);
	       paramMap.put("Psql", psql);
	       paramMap.put("Psize", psize);
	       paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
	       paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
	       this.getSqlSession().select("com.gljx.web.dao.OtherRefundBillMapper.pagemap", paramMap, null);
	       return  paramMap;
	}

	/**
	 * 取记录总数      dmz 20160921
	 * @param psql   SQL语句
	 * @return
	 */
	@Override
	public Map<String, Object> selectPageCount(String psql) {
		   Map<String, Object> paramMap = new HashMap<String, Object>();
	       paramMap.put("Psql", psql);
	       paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
	       this.getSqlSession().select("com.gljx.web.dao.OtherRefundBillMapper.pagecount", paramMap, null);
	       return paramMap;
	}

	/**
	 * 根据条件查询子类信息
	 */
	@Override
	public List<OtherRefundBillEntry> queryOtherRefundBillEntry(String fparentId,String studentName, String tkxm) {
		   Map<String, Object> paramMap = new HashMap<String, Object>();
		   paramMap.put("v_fparentid", fparentId);
	       paramMap.put("v_studentName", studentName);
	       paramMap.put("v_tkxm", tkxm);
	       paramMap.put("v_cursor", oracle.jdbc.OracleTypes.CURSOR);
	       this.getSqlSession().select("com.gljx.web.dao.OtherRefundBillEntryMapper.selectOtherRefundBillEntry", paramMap, null);
	       @SuppressWarnings("unchecked")
		   List<OtherRefundBillEntry> otherRefundBillEntrys = (List<OtherRefundBillEntry>) paramMap.get("v_cursor");
		   return otherRefundBillEntrys;
	}

	/**
	 * 添加或修改
	 */
	@Override
	public void editOtherRefundBill(OtherRefundBill otherRefundBill, String[] fseq, String[] cftkxm,
			String[] cfstudentid, String[] cfstudentno, String[] cfamount, String[] cfdesc, User user, String time) {
		// TODO Auto-generated method stub
		if("".equals(otherRefundBill.getFid()) || otherRefundBill.getFid() == null){
			//创建人id
			otherRefundBill.setFcreatorid(user.getFid());
			//利用UUID生成fid
			otherRefundBill.setFid(GetFid.UUIDIntoFid());
			//当前时间
			otherRefundBill.setFcreatetime(time);
			//状态
            otherRefundBill.setCfbillstate(3L);
            //添加
			otherRefundBillMapper.insertSelective(otherRefundBill);
		}else{
			//修改时间时间
			otherRefundBill.setFlastupdatetime(time);
			//修改人id
			otherRefundBill.setFlastupdateuserid(user.getFid());
			//修改
			otherRefundBillMapper.updateByPrimaryKeySelective(otherRefundBill);
			//先删除子类信息
			otherRefundBillEntryMapper.deleteByFparentId(otherRefundBill.getFid());
		}
		List<OtherRefundBillEntry> otherRefundBillEntrys = new ArrayList<>();
		//添加
		if(fseq == null || fseq.length == 0){
			
		}else{
			for (int i = 0; i < fseq.length; i++) {
				OtherRefundBillEntry otherRefundBillEntry = new OtherRefundBillEntry();
				otherRefundBillEntry.setCftkxm(cftkxm[i]);
				otherRefundBillEntry.setCfstudentid(cfstudentid[i]);
				otherRefundBillEntry.setCfstudentno(cfstudentno[i]);
				if("".equals(cfamount[i])){
					
				}else{
					otherRefundBillEntry.setCfamount(BigDecimalFormat.formot(cfamount[i]));
				}
				otherRefundBillEntry.setCfdesc(cfdesc[i]);
				otherRefundBillEntry.setFseq((long)i+1);
				otherRefundBillEntry.setFid(GetFid.UUIDIntoFid());
				otherRefundBillEntry.setFparentid(otherRefundBill.getFid());
				otherRefundBillEntrys.add(otherRefundBillEntry);
			}
			otherRefundBillEntryMapper.insertOtherRefundBillEntry(otherRefundBillEntrys);
		}
	}

	/**
	 * 根据fid查询
	 */
	@Override
	public OtherRefundBill queryOtherRefundBillByFid(String fid) {
		// TODO Auto-generated method stub
		return otherRefundBillMapper.selectOtherRefundBillByFid(fid);
	}

	/**
	 * 根据fid删除
	 */
	@Override
	public void deleteOtherRefundBillByFid(String[] deleteId, String fid) {
		// TODO Auto-generated method stub
		if(fid == null || "".equals(fid)){
			//先删除子类信息
			otherRefundBillEntryMapper.deleteByArray(deleteId);
			//在删除父类信息
			otherRefundBillMapper.deleteByArray(deleteId);
		}else{
			//先删除子类信息
			otherRefundBillEntryMapper.deleteByFparentId(fid);
			//在删除父类信息
			otherRefundBillMapper.deleteByPrimaryKey(fid);
		}
	}

	/**
	 * 审核，反审核
	 */
	@Override
    public int auditOtherRefundBill(String fid, String type, String fhandlerId, String message, String state, String fnumber) {
        // TODO Auto-generated method stub
		OtherRefundBill otherRefundBill = new OtherRefundBill();
        Axamine axamine = new Axamine();
        if ("1".equals(type)) {
            ServerResponse serverResponse = axamine.auditProcess(fnumber, state, message);
            if (serverResponse.getStatus() == 1) {
                otherRefundBill.setFid(fid);
                //审核人
                otherRefundBill.setFauditorid(fhandlerId);
                //修改人
                otherRefundBill.setFlastupdateuserid(fhandlerId);
                otherRefundBill.setCfbillstate(5L);
                return otherRefundBillMapper.updateByPrimaryKeySelective(otherRefundBill);
            } else if (serverResponse.getStatus() == 2) {
                //推进
                if (!"1".equals(state)) {
                    otherRefundBill.setFid(fid);
                    otherRefundBill.setCfbillstate(3L);
                    otherRefundBillMapper.updateByPrimaryKeySelective(otherRefundBill);
                }
                return 2;
            } else {
                //错误
                return 0;
            }
        }else{
            axamine.againstAuditProcess(fnumber, message);
            otherRefundBill.setCfbillstate(3L);
            otherRefundBill.setFid(fid);
            return otherRefundBillMapper.updateByPrimaryKeySelective(otherRefundBill);
        }
    }

    @Override
    public int modifyOtherRefundBill(OtherRefundBill otherRefund) {
        return otherRefundBillMapper.updateByPrimaryKeySelective(otherRefund);
    }

    @Override
    public List<OtherRefundBillEntry> selectOtherRefundBillByInfo(String fcompanyid, String studentName, String cftkxm, String beginTime, String endTime) {
        Map<String, String> map = new HashMap<>();
        map.put("studentName", studentName);
        map.put("cftkxm", cftkxm);
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        map.put("fcompanyid", fcompanyid);
        return otherRefundBillEntryMapper.selectOtherRefundBillByInfo(map);
    }
}
